<template>
  <div class="bg_block">
    <div class="index_box topHead">
      <div class="head_box">
        <div class="head_left">
          <span class="left_title">{{ t("orders") }}</span>
          <i v-if="!nowSystem" @click="goToPage('c2c')" class="iconfont icon-swap"></i>
        </div>
        <div class="head_right_box">
          <div class="head_right" @click="createOrder(1)">
            {{ t("lendOrder") }}
          </div>
          <div class="head_right" @click="createOrder(2)">
            {{ t("borrowOrder") }}
          </div>
        </div>
      </div>
      <div class="flex_box">
        <div class="complete_box">
          {{ t("Completed") }}
          <div class="complete_sum">
            <img :src="requireImg(store.state.usdtImg)" alt="" />
            <span class="sum_value">
              {{  store.state.total.usdt&&store.state.total.usdt.toFixed(0) || 0 }}
            </span>
          </div>
          <div class="complete_sum">
            <img :src="requireImg(store.state.btcImg)" alt="" />
            <span class="sum_value">
              {{ store.state.total.btc&&store.state.total.btc.toFixed(0) || 0  }}
            </span>
          </div>
        </div>
        <div class="price_box">1 {{ store.state.BTC }} ≈ {{ store.state.tokenPrice }} {{ store.state.USDT }}</div>
      </div>
      <div class="title_box">
        <div class="tab_box">
          <div class="item" :class="{ ac: active == 1 }" @click="tabChange(1)">
            {{ t('lend') }}
          </div>
          <div class="item" :class="{ ac: active == 2 }" @click="tabChange(2)">
            {{ t('borrow') }}
          </div>
          <div class="bg" :class="{ left1: active == 2 }"></div>
        </div>
        <div class="status_box">
          <van-popover v-model:show="showPopover" placement="bottom-end">
            <div class="language_box">
              <div
                class="language_item"
                v-for="item in sortList"
                :key="item.value"
                :class="{
                  language_item_active: sortStatus.value == item.value,
                }"
                @click="chooseSort(item)"
              >
                <span class="usdt_box">
                  {{ item.name }}
                </span>
              </div>
            </div>
            <template #reference>
              <span class="color_light">
                {{ sortStatus.name }}
                <i class="icon_down iconfont icon-ic_down"></i>
              </span>
            </template>
          </van-popover>
        </div>
      </div>
      <div class="order_list">
        <div><van-loading color="#1989fa" v-if="loading" /></div>
        <orderItem
          v-for="(item, index) in orderList"
          :key="index"
          :data="item"
        >
        </orderItem>
      </div>
      <van-pagination
        @change="changePage"
        v-model="currentPage"
        :total-items="store.state.orderList.length"
        :show-page-size="5"
      >
        <template #prev-text>
          <van-icon name="arrow-left" />
        </template>
        <template #next-text>
          <van-icon name="arrow" />
        </template>
        <template #page="{ text }">{{ text }}</template>
      </van-pagination>
    </div>
  </div>
  <lend-u :orderType="orderType" v-model="showlend"></lend-u>
  <borrow-u :orderType="orderType" v-model="showborrow"></borrow-u>
</template>
<script setup>
import { showToast } from "vant";
import IndexDBCache from "@/hooks/sql/useOrder.js";
const orderDB = new IndexDBCache();
import {nowSystem} from "@/utils/config.js"
import "vant/es/toast/style";
import { useStore } from "vuex";
import orderItem from "@/components/orderItem.vue";
import { ref, reactive, onMounted, computed, onUnmounted } from "vue";
import { useI18n } from "vue-i18n";
import loanEth from "@/utils/loanEth.js";
const { locale, t } = useI18n();
import { splitAddress } from "@/utils/index.js";
import { useRouter } from "vue-router";
const router = useRouter();
const store = useStore();
const showPopover = ref(false);
const active =  ref(1);
active.value = localStorage.getItem('indexActive') || 1
const sortList = computed(()=>[
  {
    value: 1,
    name: t('defaultSort1'),
  },
  {
    value: 2,
    name: t('defaultSort2'),
  },
  {
    value: 3,
    name: t('defaultSort3'),
  },
  {
    value: 4,
    name: t('defaultSort4'),
  },
  {
    value: 5,
    name: t('defaultSort5'),
  },
  {
    value: 6,
    name:t('defaultSort6'),
  },
]);
const sortStatus = ref({});
sortStatus.value = sortList.value[0];
const chooseSort = (item) => {
  sortStatus.value = item;
  showPopover.value = false;
};
const changePage = ()=>{
}
const loading = ref(false);
const orderStatus = ref("0");
const orderData = ref({});
const currentPage = ref(1);
const pageSize = 10;
const showlend = ref(false);
const showborrow = ref(false);
const orderType = ref(1);
const createOrder = (type) => {
  orderType.value = type;
  if (type == 1) {
    showlend.value = true;
  } else {
    showborrow.value = true;
  }
};
const orderList = computed(()=>{
  let sortFn = {
    1:(n,m)=>{return n.createTime-m.createTime},
    2:(n,m)=>{return active.value==1? m.amount-n.amount:m.amountBTC-n.amountBTC},
    3:(n,m)=>{ return active.value==1? n.amount-m.amount:n.amountBTC-m.amountBTC},
    4:(n,m)=>{ return n.interestRate-m.interestRate},
    5:(n,m)=>{ return n.duration-m.duration},
    6:(n,m)=>{ return m.duration-n.duration},

  }
  return store.state.orderList.filter((res)=>res.orderType==(active.value==1?'2':'1')).sort(sortFn[sortStatus.value.value]).slice((currentPage.value - 1) * pageSize, (currentPage.value - 1) * pageSize + pageSize)
})
const tabChange=(value)=>{
  active.value = value
  localStorage.setItem('indexActive',value)
}
const requireImg = (imgPath) => {
  return new URL(`../../assets/images/${imgPath}`, import.meta.url).href;
};
const initData = (accounts) => {
  loanEth.getPendingLoanOrders()
};
const goToPage = (value, type) => {
  let query = {
  };
  router.push({
    name: value,
    query,
  });
};
onMounted(() => {
  initData();
});
</script>
<style>
.order_list{
  min-height: 80px;
}
:root:root {
  --van-button-primary-background: red;
  --van-pagination-item-default-color: #f39018;
  --van-pagination-disabled-opacity: #010e39 !important;
}
</style>
<style lang="scss" scoped>
.title_box {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.price_box {
  color: #fff;
}
.tab_box {
  position: relative;
  background: #131e3f;
  border-radius: 6px;
  display: inline-flex;
  .item {
    text-align: center;
    width: 60px;
    height: 30px;
    line-height: 30px;
    position: relative;
    z-index: 2;
    color: #c3c8d2;
  }
  .ac {
    color: #fff;
  }
  .bg {
    position: absolute;
    left: 4px;
    top: 2px;
    z-index: 1;
    width: 54px;
    height: 26px;
    background: #f39018;
    border-radius: 6px;
    transition: all 0.5s;
  }
  .left1 {
    left: 62px;
  }
}
:deep .van-pagination__item--disabled {
  background: #010e39;
}
.language_box {
  padding: 5px 0px;
  background: #222143;
  position: relative;
}
.language_item {
  font-size: 12px;
  padding: 10px 15px;
  cursor: pointer;
  text-align: center;
  display: flex;
  align-items: center;

  .van-image {
    height: 18px;
    width: 18px;
    margin-right: 8px;
  }
}
.language_item_active {
  color: rgb(246, 109, 26);
}
.status_box {
  position: relative;
  .css-qmhmz {
    color: #bab9b9;
    background: #010e39;
    font-family: Rubik;
    font-weight: 400;
    font-size: 12px;
    line-height: 1.4375em;
    letter-spacing: 0.00938em;
    text-transform: none;
    display: block;
    transform-origin: left top;
    text-overflow: ellipsis;
    max-width: calc(133% - 32px);
    position: absolute;
    left: 0px;
    top: 0px;
    transform: translate(6px, -8px) scale(0.75);
    z-index: 1;
    pointer-events: auto;
    user-select: none;
    padding: 0px;
    white-space: nowrap;
    overflow: hidden;
    transition: color 200ms cubic-bezier(0, 0, 0.2, 1),
      transform 200ms cubic-bezier(0, 0, 0.2, 1),
      max-width 200ms cubic-bezier(0, 0, 0.2, 1);
  }

  display: flex;
  align-items: center;
  justify-content: center;

  .color_light {
    color: #ddd;
    display: inline-block;
    border: 1px solid #414c60;
    position: relative;
    padding: 6px;
    margin-left: 10px;
    border-radius: 5px;
    display: flex;
    align-items: center;
    .icon_down {
      font-size: 10px;
      margin-left: 4px;
    }
    img {
      height: 16px;
      margin-right: 7px;
    }
  }
}
.index_box {
  padding: 12px;
  .head_box {
    display: flex;
    align-items: center;
    // justify-content: space-between;
    .head_left {
      display: flex;
      align-items: center;
      .left_title {
        font-size: 26px;
        font-weight: bold;
        color: #f39018;
      }
      .icon_refresh {
        color: #c3c8d2;
        margin-left: 10px;
        font-size: 10px;
      }
    }
    .head_right_box {
      margin-left: auto;
      display: flex;
      gap: 10px;
    }
    .head_right {
      background: #f39018;
      color: #333;
      font-weight: bold;
      padding: 6px;
      border-radius: 6px;
    }
  }
  .complete_box {
    display: flex;
    align-items: center;
    margin: 5px 0;
    justify-content: center;

    .complete_sum {
      display: flex;
      margin-left: 10px;
      align-items: center;
      gap: 6px;
      img {
        height: 16px;
      }
      .sum_value {
        color: #f39018;
      }
    }
  }
}
</style>
